﻿using System.Web.Mvc;
using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System.IO;
using System.Collections.Generic;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.Threading.Tasks;

namespace Pay.Web.Areas.Admin.Controllers
{
    [RouteArea("admin")]
    [RoutePrefix("order")]
    public class LogisticsController : AdminController
    {
        private ILogisticsService _iLogisticsService;
        private ILogisticsListService _iLogisticsListService;
        private IPayInfoService _iPayInfoService;
        public LogisticsController(ILogisticsService iLogisticsService, ILogisticsListService iLogisticsListService, IPayInfoService iPayInfoService) {
            _iLogisticsService = iLogisticsService;
            _iLogisticsListService = iLogisticsListService;
            _iPayInfoService = iPayInfoService;
        }

        /// <summary>
        /// 获取物流订单列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public JsonResult Gets(LogiticsQuery query) {
            query.logistics_status = LogisticsStatuss.FinishFlight;
            var LogisticsLists = _iLogisticsListService.Gets(query);
            foreach (var model in LogisticsLists.rows) {
                var pay_infos = _iPayInfoService.GetListByBoardId(model.id);
                if (pay_infos != null)
                    model.payinfos = pay_infos;
            }
            return Json(ResultToJson.Json(LogisticsLists), JsonRequestBehavior.AllowGet);
        }
        /// <summary>
        /// 推送订单
        /// </summary>
        /// <returns>The order.</returns>
        /// <param name="pay_id">pay_id.</param>
        public JsonResult PushOrder(long pay_id) {
			bool result = _iLogisticsService.PushOrder(pay_id);
			return Json((result ? "推送成功" : "推送失败"), JsonRequestBehavior.AllowGet);
		}
        /// <summary>
        /// 推送支付单
        /// </summary>
        /// <returns>The pay order.</returns>
        /// <param name="pay_id">pay_id.</param>
		public async Task<JsonResult> PushPayOrder(long pay_id) {
			bool result = await _iLogisticsService.PushPayOrder(pay_id);
			return Json(result ? "推送成功" : "推送失败", JsonRequestBehavior.AllowGet);
		}

        /// <summary>
        /// 生成物流运单/清单Excel表格
        /// </summary>
        /// <returns>The excel.</returns>
        /// <param name="logistics_id">Pay identifier.</param>
        /// <param name="type">Type.</param>
        public FileResult LogisticsExcel(long logistics_id, int type) {
            LogisticsExcel excel = new LogisticsExcel();
            var pay_Infos = _iPayInfoService.GetListByBoardId(logistics_id);
            if (type == 1)
                excel = _iLogisticsService.LogisticsNoExcel(pay_Infos);
            else
                excel = _iLogisticsService.LogisticsDetailExcel(pay_Infos);
            return File(excel.ms.ToArray(), excel.ContentType, excel.ExcelName);
        }
    }
}